package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.PageInfo;
import com.cskaoyan.bean.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 * 请求参数名和Handler方法的形参名一致
 * @author stone
 * @date 2022/06/21 15:05
 */
@RestController
@RequestMapping("user")
public class UserController {

    // localhost:8080/user/register1?username=songge&password=niupi&age=30
    @RequestMapping("register1")
    public BaseRespVo register1(String username, String password, String age) {
        //Integer intAge = Integer.parseInt(age);
        return BaseRespVo.ok(username + ":" + password + "-->" + age);
    }

    // 相较于上面的register1修改了age的类型 → 建议使用包装类来接收，避免空值转换异常
    // localhost:8080/user/register2?username=songge&password=niupi&age=30
    @RequestMapping("register2")
    public BaseRespVo register2(String username, String password, int age) {

        return BaseRespVo.ok(username + ":" + password + "-->" + age);
    }

    // 如果要携带数组数据 → 多个请求参数名相同
    // localhost:8080/user/register3?username=songge&password=niupi&age=30
    // &hobbies=sing&hobbies=dance&hobbies=rap&ids=1&ids=2&ids=3
    @RequestMapping("register3")
    public BaseRespVo register3(String username, String password, Integer age,
                                String[] hobbies, Integer[] ids) {
        System.out.println(Arrays.asList(ids));
        return BaseRespVo.ok(hobbies);
    }
    // 日期按照默认的格式yyyy/MM/dd提供
    // localhost:8080/user/register4?username=songge&password=niupi&age=30
    // &hobbies=sing&hobbies=dance&hobbies=rap&ids=1&ids=2&ids=3
    // &birthday=2022/06/21
    @RequestMapping("register4")
    public BaseRespVo register4(String username, String password, Integer age,
                                String[] hobbies, Integer[] ids,
                                Date birthday) {


        return BaseRespVo.ok(birthday);
    }
    // 日期提供一个自定义格式的日期，并且接收的时候指定
    // localhost:8080/user/register5?username=songge&password=niupi&age=30
    // &hobbies=sing&hobbies=dance&hobbies=rap&ids=1&ids=2&ids=3
    // &birthday=2022-06-21
    @RequestMapping("register5")
    public BaseRespVo register5(String username, String password, Integer age,
                                String[] hobbies, Integer[] ids,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {


        return BaseRespVo.ok(birthday);
    }

    // 构造一个和register5请求参数完全一模一样的请求
    // localhost:8080/user/register6?username=songge&password=niupi&age=30
    // &hobbies=sing&hobbies=dance&hobbies=rap&ids=1&ids=2&ids=3
    // &birthday=2022-06-21
    @RequestMapping("register6")
    public BaseRespVo register6(User user) {


        return BaseRespVo.ok();
    }


    //localhost:8080/user/query?age=20&page=1&limit=10
    @RequestMapping("query")
    public BaseRespVo query(Integer age, PageInfo pageInfo) {
        return BaseRespVo.ok();
    }
}
